package com.xjtu.backend.result;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> {
    private Integer code;

    private String message;
    private T data;

    public static final Integer SUCCESS = ResponseEnum.SUCCESS.getCode();
    public static final Integer FAILD = ResponseEnum.FAILD.getCode();

    private static <T> Result<T> restResult(T data, int code, String message) {
        Result<T> apiResult = new Result<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMessage(message);
        return apiResult;
    }


    /**
     * 返回 成功与失败结果
     */
    public static <T> Result<T> ok() {
        return restResult(null, SUCCESS, ResponseEnum.SUCCESS.getMessage());
    }

    public static <T> Result<T> ok(T data) {
        return restResult(data, SUCCESS, ResponseEnum.SUCCESS.getMessage());
    }

    public static <T> Result<T> ok(T data, String msg) {
        return restResult(data, SUCCESS, msg);
    }


    /**
     * 返回 失败结果
     */
    public static <T> Result<T> error() {
        return restResult(null, FAILD, ResponseEnum.FAILD.getMessage());
    }

    public static <T> Result<T> error(String msg) {
        return restResult(null, FAILD, msg);
    }

    public static <T> Result<T> error(T data) {
        return restResult(data, FAILD, ResponseEnum.FAILD.getMessage());
    }

    public static <T> Result<T> error(T data, String msg) {
        return restResult(data, FAILD, msg);
    }

    public static <T> Result<T> error(Integer code, String msg) {
        return restResult(null, code, msg);
    }

    /**
     * 返回 指定结果
     */
    public static <T> Result<T> setResult(IResponseEnum responseEnum) {
        return restResult(null, (Integer) responseEnum.code(), (String) responseEnum.message());
    }

    public static <T> Result<T> setResult(IResponseEnum responseEnum, T data) {
        return restResult(data, (Integer) responseEnum.code(), (String) responseEnum.message());
    }


}
